package com.huaishushu.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaishushu.common.R;
import com.huaishushu.entity.Employee;
import com.huaishushu.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        String password = employee.getPassword();
        //使用DigestUtils工具类进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名进行查询
        Employee emp = employeeService.login(employee.getUsername());

        if (emp == null) {
            return R.error("登录失败，账号或密码错误！！！");
        }
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败，账号或密码错误！！！");
        }
        //查询员工状态，如果为已禁用状态，则放回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());

        return R.success(emp);
    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        log.info("用户已退出，退出的id："+request.getSession().getAttribute("employee"));
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");

        return R.success("退出成功");
    }

    /**
     * 新增员工信息
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        if (employeeService.save(request, employee)) {
            log.info("新增员工信息："+employee);
            return R.success("新增员工成功");
        }

        return R.error("新增员工失败");
    }


    /**
     * 员工信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Employee> pageInfo = employeeService.page(page, pageSize, name);
        log.info("员工信息分页查询");
        return R.success(pageInfo);
    }

    /**
     * 启用/禁用员工账号
     * 修改员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest resource, @RequestBody Employee employee) {
        Long id = Thread.currentThread().threadId();
        log.info("线程：{}",id);
        if (employeeService.update(resource, employee)) {
            if (employee.getStatus() == 1) {
                return R.success("启用成功");
            } else {
                return R.success("禁用成功");
            }
        }
        return R.error("操作失败");
    }

    /**
     * 根据id查询员工信息
     * 其时搭配编辑员工信息使用的
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable("id") Long id) {
        Employee employee = employeeService.getById(id);
        log.info("根据id查询到的员工信息："+employee);
        if (employee != null) {
            return R.success(employee);
        }
        //这种情况只有在页面刷新过慢时会出现，或出现线程安全时出现
        return R.error(null);
    }

}
